package com.yuxl.common.auth.repository;

import com.yuxl.common.auth.entity.AuthPermission;
import com.yuxl.common.auth.entity.AuthPermission;
import com.yuxl.common.core.annotation.PostgreSqlRepository;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import com.yuxl.common.core.annotation.PostgreSqlRepository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @Author : yuxl
 * @create 2025/7/22 下午5:09
 */
@PostgreSqlRepository
public interface AuthPermissionRepository extends ReactiveCrudRepository<AuthPermission, String> {
    @Query("""
            SELECT count(*) FROM auth_permission
            WHERE 1=1
            AND (:#{#entity.name} IS NULL OR name LIKE CONCAT('%', :#{#entity.name}, '%'))
            """)
    Mono<Integer> count(AuthPermission entity);

    @Query("""
            SELECT * FROM auth_permission
            WHERE 1=1
            AND (:#{#entity.name} IS NULL OR name LIKE CONCAT('%', :#{#entity.name}, '%'))
            ORDER BY create_time DESC
            LIMIT :limit OFFSET :offset
            """)
    Flux<AuthPermission> page(AuthPermission entity, long limit, long offset);

    @Query("""
            SELECT * FROM auth_permission
            WHERE 1=1
            AND (:#{#entity.name} IS NULL OR name LIKE CONCAT('%', :#{#entity.name}, '%'))
            ORDER BY create_time DESC
            """)
    Flux<AuthPermission> list(AuthPermission entity);


    // 添加根据id 的 update语句
    @Query("""
            UPDATE auth_permission
            SET name = :#{#entity.name},
                update_time = :#{#entity.updateTime}
            WHERE mark = :#{#entity.mark}
            RETURNING *
            """)
    Mono<AuthPermission> updateById(AuthPermission entity);
}
